package ar.com.spsolutions.splibrary.services;

import java.util.List;

import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;

import ar.com.spsolutions.splibrary.book.Book;
import ar.com.spsolutions.splibrary.book.category.Category;
import ar.com.spsolutions.splibrary.general.IFindByNameService;
import ar.com.spsolutions.splibrary.general.RoleConverterService;
import ar.com.spsolutions.splibrary.repositories.CategoryRepository;

public class CategoryService extends TemplateEntityService<Category> implements
		IFindByNameService<Category> {

	private static final long serialVersionUID = 775793052420125497L;

	@Override
	@Secured(value = { RoleConverterService.ROLE_LIBRARIAN })
	public void add(final Category aEntity) {
		super.add(aEntity);
	}

	private CategoryRepository getMyRepository() {
		return (CategoryRepository) this.getRepository();
	}

	@Transactional(readOnly = true)
	public boolean exist(final Category category) {
		return this.getMyRepository().exist(category);
	}

	@Transactional(readOnly = true)
	public Category findByName(final String name) {
		return this.getMyRepository().findByName(name);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Category> findByLikeName(final String name) {
		return this.getRepository().findByLike("name", name);
	}

	@Transactional(readOnly = true)
	public int sizeOfSearch(final String searchValue) {
		return this.getMyRepository().sizeOfSearch(searchValue);
	}

	@Transactional(readOnly = true)
	public List<? extends Category> getResults(final String searchValue,
			final int first, final int count) {
		return this.getMyRepository().getResults(searchValue, first, count);
	}

	@Transactional(readOnly = true)
	public List<Book> getBooksFor(final Category category) {
		return this.getMyRepository().getBooksFor(category);
	}

}
